#include "widget.h"
#include "ui_widget.h"
#include<QDebug>
#include<fstream>
#include<QShortcut>
Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    ui->comboBox->addItem("香辣鸡腿堡");
    ui->comboBox->addItem("双鸡煲");
    ui->comboBox->addItem("田园堡");

    ui->comboBox_2->addItem("可乐");
    ui->comboBox_2->addItem("雪碧");

    ui->comboBox_3->addItem("薯条");
    ui->comboBox_3->addItem("鸡块");

    std::ifstream file("D:/QT SDK/qt/2024-10-28/com.txt");
    if(!file.is_open())
    {
        qDebug()<<"文件打开失败";
    }
    std::string line;
    while(std::getline(file,line))
    {
        ui->comboBox_4->addItem(QString::fromStdString(line));
    }
    file.close();
    ui->spinBox->setRange(1,5);
    ui->spinBox->setValue(1);
    ui->spinBox->setSuffix("个");

    ui->spinBox_2->setRange(1,5);
    ui->spinBox_2->setValue(1);
    ui->spinBox_2->setSuffix("杯");

    ui->spinBox_3->setRange(1,5);
    ui->spinBox_3->setValue(1);
    ui->spinBox_3->setSuffix("盒");

    ui->horizontalScrollBar->setMinimum(800);
    ui->horizontalScrollBar->setMaximum(8000);
    ui->horizontalScrollBar->setValue(2000);
    ui->horizontalScrollBar->setSingleStep(50);
     ui->verticalScrollBar->setMinimum(600);
     ui->verticalScrollBar->setMaximum(8000);
     ui->verticalScrollBar->setValue(2000);
     ui->verticalScrollBar->setSingleStep(50);

     QShortcut* shortcut=new QShortcut(this);
     shortcut->setKey(QKeySequence("-"));
     QShortcut* shortcut2=new QShortcut(this);
     shortcut2->setKey(QKeySequence("+"));
     connect(shortcut,&QShortcut::activated,this,&Widget::cutvalue);
     connect(shortcut2,&QShortcut::activated,this,&Widget::addvalue);

     ui->listWidget->addItem("C++");
     ui->listWidget->addItem("Java");
     ui->listWidget->addItem("python");

     ui->tableWidget->insertRow(0);
     ui->tableWidget->insertRow(1);
     ui->tableWidget->insertRow(2);

     ui->tableWidget->insertColumn(0);
     ui->tableWidget->insertColumn(1);
     ui->tableWidget->insertColumn(2);

     ui->tableWidget->setHorizontalHeaderItem(0,new QTableWidgetItem("学号"));
     ui->tableWidget->setHorizontalHeaderItem(1,new QTableWidgetItem("姓名"));
     ui->tableWidget->setHorizontalHeaderItem(2,new QTableWidgetItem("年龄"));

     ui->tableWidget->setItem(0,0,new QTableWidgetItem("2001"));
     ui->tableWidget->setItem(0,1,new QTableWidgetItem("张三"));
     ui->tableWidget->setItem(0,2,new QTableWidgetItem("20"));

     ui->tableWidget->setItem(1,0,new QTableWidgetItem("2002"));
     ui->tableWidget->setItem(1,1,new QTableWidgetItem("李四"));
     ui->tableWidget->setItem(1,2,new QTableWidgetItem("19"));

     ui->tableWidget->setItem(2,0,new QTableWidgetItem("2003"));
     ui->tableWidget->setItem(2,1,new QTableWidgetItem("王五"));
     ui->tableWidget->setItem(2,2,new QTableWidgetItem("18"));

}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_pushButton_clicked()
{
    //qDebug()<<ui->comboBox->currentText()<<","<<ui->comboBox_2->currentText()<<","<<ui->comboBox_3->currentText()<<".";
    qDebug()<<"你选择的食物："
            <<ui->comboBox->currentText()<<ui->spinBox->value()<<"个"
           <<ui->comboBox_2->currentText()<<ui->spinBox_2->value()<<"杯"
          <<ui->comboBox_3->currentText()<<ui->spinBox_3->value()<<"盒";
}

void Widget::on_pushButton_2_clicked()
{
    QDateTime timeold=ui->dateTimeEdit->dateTime();
    QDateTime timenew=ui->dateTimeEdit_2->dateTime();
    //int days=timeold.daysTo(timenew);
    int secends=timeold.secsTo(timenew);
    int hours=(secends/3600)%24;
    int days=(secends/3600)/24;
    ui->label_4->setText(QString("已经过了")+QString::number(days)+QString("天")+QString::number(hours)+QString("小时"));
}

void Widget::on_dial_valueChanged(int value)
{
    this->setWindowOpacity((double)value/100);
}

void Widget::on_horizontalScrollBar_valueChanged(int value)
{
    QRect rect=this->geometry();
    this->setGeometry(rect.x(),rect.y(),value,rect.height());
}

void Widget::on_verticalScrollBar_valueChanged(int value)
{
    QRect rect=this->geometry();
    this->setGeometry(rect.x(),rect.y(),rect.width(),value);
}

void Widget::cutvalue()
{
    int value=ui->horizontalScrollBar_2->value();
    if(value<=ui->horizontalScrollBar_2->minimum())
    {
        return ;
    }
    ui->horizontalScrollBar_2->setValue(value-5);
}

void Widget::addvalue()
{
    int value=ui->horizontalScrollBar_2->value();
    if(value>=ui->horizontalScrollBar_2->maximum())
    {
        return ;
    }
    ui->horizontalScrollBar_2->setValue(value+5);
}


void Widget::on_pushButton_add_clicked()
{
    const QString& text=ui->lineEdit->text();
    ui->listWidget->addItem(text);
}

void Widget::on_pushButton_delete_clicked()
{
    int row=ui->listWidget->currentRow();
    if(row<0)
    {
        return;
    }
    ui->listWidget->takeItem(row);
}

void Widget::on_listWidget_currentItemChanged(QListWidgetItem *current, QListWidgetItem *previous)
{
    if(current!=nullptr)
    {
        qDebug()<<"当前选中的元素 "<<current->text();
    }
    if(previous!=nullptr)
    {
        qDebug()<<"上次选中的元素 "<<previous->text();
    }
}

void Widget::on_pushButton_insert_row_clicked()
{
    int row=ui->tableWidget->rowCount();
    ui->tableWidget->insertRow(row);
}

void Widget::on_pushButton_insert_colomn_clicked()
{
    int column=ui->tableWidget->columnCount();
    ui->tableWidget->insertColumn(column);
    const QString& text=ui->lineEdit_2->text();
    ui->tableWidget->setHorizontalHeaderItem(column,new QTableWidgetItem(text));
}

void Widget::on_pushButton_delete_row_clicked()
{
    int row=ui->tableWidget->currentRow();
    ui->tableWidget->removeRow(row);
}

void Widget::on_pushButton_delete_colomn_clicked()
{
    int column=ui->tableWidget->currentColumn();
    ui->tableWidget->removeColumn(column);
}
